

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;

// https://go.microsoft.com/fwlink/?LinkId=402352&clcid=0x804 上介绍了“空白页”项模板


using Windows.Devices.Bluetooth;
using Windows.Devices.Bluetooth.GenericAttributeProfile;
using Windows.Devices.Enumeration;
using Windows.UI.Core;
using System.Text;
using Windows.Security.Cryptography;
using Windows.Storage.Streams;
using Windows.Devices.Bluetooth.Advertisement;

namespace App1
{
    public class DBG_GOBAL
    {
        public static void WriteLine(String msg) 
        {    
           //await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
           // {
                System.Diagnostics.Debug.WriteLine(msg);
            //});

        }
    }

	public class NutCharaceristic
	{
		private DeviceInformation deviceInfor = null;

		public NutCharaceristic(DeviceInformation deviceInfor)
		{
			this.deviceInfor = deviceInfor;
		}
	}

	public class NutBle
	{
		private DeviceWatcher deviceWatcher = null;
		private BluetoothLEDevice bluetoothLeDevice = null;
		private DeviceInformation deviceInformation = null;
		private DevicePairingResult result = null;
		private GattCharacteristic csc_characteristic = null;

		private List<DeviceInformation> devices;

		public NutBle(){}

        private void dev_watcher()
        {
            // Additional properties we would like about the device.
            string[] requestedProperties = { "System.Devices.Aep.DeviceAddress", "System.Devices.Aep.IsConnected" };

            // BT_Code: Currently Bluetooth APIs don't provide a selector to get ALL devices that are both paired and non-paired.
            deviceWatcher =
                    DeviceInformation.CreateWatcher(
                        "(System.Devices.Aep.ProtocolId:=\"{bb7bb05e-5972-42b5-94fc-76eaa7084d49}\")",
                        requestedProperties,
                        DeviceInformationKind.AssociationEndpoint);

            // Register event handlers before starting the watcher.
            deviceWatcher.Added += DeviceWatcher_Added;
            deviceWatcher.Updated += DeviceWatcher_Updated;
            deviceWatcher.Removed += DeviceWatcher_Removed;
            deviceWatcher.EnumerationCompleted += DeviceWatcher_EnumerationCompletedAsync;
            deviceWatcher.Stopped += DeviceWatcher_Stopped;

            // Start the watcher.
            deviceWatcher.Start();
        }


                private async void DeviceWatcher_Added(DeviceWatcher sender, DeviceInformation deviceInfo)
        {
            //var service = await GattDeviceService.FromIdAsync(args.Id);
            //if (service != null)
            //{
            //    switch (service.Uuid.ToString())
            //    {
            //        case SensorUUIDs.UUID_ENV_SERV:
            //            InitializeTemperatureSensor(service);
            //            InitializePressureSensor(service);
            //            break;
            //        case SensorUUIDs.UUID_ACC_SERV:
            //            InitializeAccelerationSensor(service);
            //            break;
            //    }
            //}
            if (sender == deviceWatcher)
            {
                devices.Add(deviceInfo);
            }
            //    // We must update the collection on the UI thread because the collection is databound to a UI element.
            //    await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
            //{
            //    // Protect against race condition if the task runs after the app stopped the deviceWatcher.
            //    if (sender == deviceWatcher)
            //    {
            //    	devices.append(deviceInfo);
            //    	// new NutCharaceristic(deviceInfo)
            //        // testbox1.Text += deviceInfo.Id;
            //        // ble_id = deviceInfo.Id;
            //        // deviceInformation = deviceInfo;
            //        //// Make sure device name isn't blank or already present in the list.
            //        //if (deviceInfo.Name != string.Empty && FindBluetoothLEDeviceDisplay(deviceInfo.Id) == null)
            //        //{
            //        //    ResultCollection.Add(new BluetoothLEDeviceDisplay(deviceInfo));
            //        //}
            //    }
            //});
        }

        private async void DeviceWatcher_Updated(DeviceWatcher sender, DeviceInformationUpdate deviceInfoUpdate)
        {
        }

        private async void DeviceWatcher_Removed(DeviceWatcher sender, DeviceInformationUpdate deviceInfoUpdate)
        {
        }

        private async void DeviceWatcher_EnumerationCompletedAsync(DeviceWatcher sender, object e)
        {
        }

        private async void DeviceWatcher_Stopped(DeviceWatcher sender, object e)
        {
            DBG_GOBAL.WriteLine("watcher stopped call");
        }

		public void scan_dev()
		{

		}

		public void pair_dev()
		{

		}

		public void unpair_dev()
		{

		}

		public void disconn_dev()
		{

		}

		public void conn_dev()
		{

		}

		public void list_service()
		{

		}

		public void list_service_characteristic()
		{

		}

	}


}